import { getConfigList, getConfigSearchList } from '@/services';
import { RecordsObject } from '@/types';
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';

export interface ConfigModelState {
    ConfigList: RecordsObject[],
    SearchConfigList: RecordsObject[]
}
    

export interface ConfigModelType {
    namespace: 'config';
    state: ConfigModelState;
    effects: {
      getConfigList: Effect;
      getConfigSearchList: Effect;
    };
    reducers: {
        save: Reducer<ConfigModelState>;
    };
}

const ConfigModel: ConfigModelType = {
    namespace: 'config',

    state: {
        //归档列表数据
        ConfigList: [],   
        SearchConfigList: []    
    },

    effects: {
        // 归档列表
        *getConfigList({ payload }, { call, put }) {
            const result = yield call(getConfigList,payload);
            localStorage.setItem('configList',JSON.stringify(result.records));
            if (result) {
                yield put({
                    type: "save",
                    payload: {
                      ConfigList: result.records,                       
                    }
                })
            }
        },

        *getConfigSearchList({ payload }, { call, put }) {
            const result = yield call(getConfigSearchList,payload);
            console.log(result,'result......')
            localStorage.setItem('configSearchList',JSON.stringify(result.records));
            if (result) {
                yield put({
                    type: "save",
                    payload: {
                      SearchConfigList: result.records,                       
                    }
                })
            }
        },
    },
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
    },
};

export default ConfigModel;


